package com.newbee.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hhu.newbee.R;
import com.newbee.listbean.FriendsAskBean;
import com.newbee.smack.SmackClient;
import com.newbee.sqlite.DBHelper;

public class AskFriendsListViewAdapter extends BaseAdapter
{

	private static final int MAX_LENGTH = 20;

	private Context context;

	private List<FriendsAskBean> list;

	public AskFriendsListViewAdapter(Context context, List<FriendsAskBean> list)
	{
		super();
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		if (this.list != null)
		{
			return list.size();
		}
		return 0;

	}

	@Override
	public Object getItem(int arg0)
	{
		return null;
	}

	@Override
	public long getItemId(int arg0)
	{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int posi, View v, ViewGroup vg)
	{

		if (v == null)
		{
			v = LayoutInflater.from(context).inflate(
					R.layout.listitem_ask_friends, null);
		}
		LayoutBean view = new LayoutBean();

		FriendsAskBean bean = list.get(posi);
		view.statusTextView = (TextView) v.findViewById(R.id.ask_friend_status);
		view.fromTextView = (TextView) v.findViewById(R.id.ask_friend_from);
		view.img = (ImageView) v.findViewById(R.id.askfriend_img);
		view.statusTextView
				.setText(bean.getTag() == DBHelper.UN_SOLVE ? "[未处理]" : "[已处理]");
		String str = bean.getFromFriend().split("\\$")[1];
		// String pureStr = str.substring(0, str.length() -
		// SmackClient.SERVER_NAME_LENGTH - 1);
		// pureStr = pureStr.replace("\\40", "@");
		// int end = pureStr.length() < MAX_LENGTH ? pureStr.length() :
		// MAX_LENGTH;
		// view.fromTextView.setText(pureStr.subSequence(0, end));

		view.fromTextView.setText("有来自" + str + "加好友请求");

		if (bean.getTag() == DBHelper.UN_SOLVE)
		{
			view.img.setImageDrawable(context.getResources().getDrawable(
					R.drawable.caution_unsolve));

		}
		else
		{
			view.img.setImageDrawable(context.getResources().getDrawable(
					R.drawable.caution_solve));
		}

		return v;
	}

	static class LayoutBean
	{
		TextView statusTextView;
		TextView fromTextView;
		ImageView img;
	}

}
